package com.xjscrm.console.web.corp;


import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.common.util.LoginContext;
import com.xjscrm.console.dto.corp.WwCorpDTO;
import com.xjscrm.console.param.BaseDto;
import com.xjscrm.console.param.QueryParam;
import com.xjscrm.console.service.customer.corp.WwCorpService;
import com.xjscrm.console.vo.CircleTaskVO;
import com.xjscrm.console.vo.LoginVO;
import com.xjscrm.console.vo.WwCorpVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

/**
 * @author szl
 * @since 2024-04-09 10:27:53
 */
@RestController
@RequestMapping("/corp")
@Api(tags = "企业相关控制器")
public class CorpController {
    /**
     * 服务对象
     */
    @Autowired
    private WwCorpService wwCorpService;

    /**
     * 分页查询所有数据
     *
     * @param source 查询实体
     * @return 所有数据
     */
    @PostMapping("/search")
    @ApiOperation(value = "企业分页查询", response = WwCorpVO.class)
    public Object search(@RequestBody QueryParam source) {
        if (StringUtils.isBlank(source.getOrderByField())) {
            source.setOrderByField("create_time");
            source.setAsc(false);
        }
        LoginVO login = LoginContext.getLogin();
        return wwCorpService.page(login.getMerId(), source);
    }

    @PostMapping("/callbackUrl")
    @ApiOperation(value = "查看企业回调地址", response = WwCorpVO.class)
    public Object callback(@RequestBody WwCorpDTO source) {
        if (Objects.isNull(source) || Objects.isNull(source.getId())) {
            throw new BusinessException("查看回调机器参数错误");
        }
        LoginVO login = LoginContext.getLogin();
        return wwCorpService.callbackUrl(login.getMerId(), source.getId());
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @PostMapping("/get")
    @ApiOperation(value = "企业详情", response = CircleTaskVO.class)
    public Object get(@RequestBody WwCorpDTO source) {
        if (Objects.isNull(source) || Objects.isNull(source.getId())) {
            throw new BusinessException("企业详情参数错误");
        }
        LoginVO login = LoginContext.getLogin();
        return this.wwCorpService.get(login.getMerId(), source.getId());
    }


    /**
     * 修改数据
     *
     * @param source 实体对象
     * @return 修改结果
     */
    @PostMapping("/add")
    @ApiOperation("新增企业信息")
    public Object add(@RequestBody WwCorpDTO source) {
        if (Objects.isNull(source) || StringUtils.isAnyBlank(source.getCorpId(),
                source.getEntId(), source.getEntSecret(), source.getEntToken(), source.getName())) {
            throw new BusinessException("新增企业信息参数错误");
        }
        LoginVO login = LoginContext.getLogin();
        return this.wwCorpService.add(login.getMerId(), source);
    }

    /**
     * 修改数据
     *
     * @param source 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation("修改企业信息")
    public Object update(@RequestBody WwCorpDTO source) {
        if (Objects.isNull(source) || Objects.isNull(source.getId())) {
            throw new BusinessException("修改企业信息参数错误");
        }
        LoginVO login = LoginContext.getLogin();
        return this.wwCorpService.edit(login.getMerId(), source);
    }


    @PostMapping("/remove")
    @ApiOperation(value = "删除企业信息", response = Boolean.class)
    public Object remove(@RequestBody WwCorpDTO source) {
        LoginVO login = LoginContext.getLogin();
        return this.wwCorpService.remove(login.getMerId(), source.getId());
    }

    @PostMapping("/getDropDown")
    @ApiOperation(value = "获取企业下拉", response = Map.class)
    public Object getDropDown(@RequestBody BaseDto baseDto) throws Exception {
        return wwCorpService.getDropDown(baseDto);
    }


    @PostMapping("/checkAuth")
    @ApiOperation(value = "检查授权", response = Boolean.class)
    public Object checkAuth(@RequestBody WwCorpDTO source) throws Exception {
        if (Objects.isNull(source) || Objects.isNull(source.getId())) {
            throw new BusinessException("企业检查授权参数错误");
        }
        LoginVO login = LoginContext.getLogin();
        return wwCorpService.checkAuth(login.getMerId(), source.getId());
    }
}

